#pragma warning(disable:4530)
#define WIN32_LEAN_AND_MEAN
#include "MD5Stream.h"
#include "Log.h"
//extern CLog Loger;
static char hexmap[] = "0123456789abcdef";

MD5Stream::MD5Stream()  
{
	MD5_Init(&m_Context);
}

MD5Stream::~MD5Stream()
{}

mystring MD5Stream::getHex()
{
//	Loger.printf(LOG_MSG,"%s\r\n",m_sContent.c_str());
	MD5_Update(&m_Context,(UInt8*)m_sContent.c_str(),m_sContent.length());
	MD5_Final((UInt8*)m_Buf, &m_Context);
	mystring digest = "";//((const char*)mBuf,16, false);
	char* p = m_Buf;	
	for (int i=0; i < 16; i++)
	{
		unsigned char temp = *p++;
	   
		int hi = (temp & 0xf0)>>4;
		int low = (temp & 0xf);
      
		digest += hexmap[hi];
		digest += hexmap[low];
	}	
	return digest;
}
MD5Stream &MD5Stream::operator<<(const char *pstr)
{
	if(pstr)
		m_sContent += pstr;
	return *this;
}
MD5Stream &MD5Stream::operator<<(const mystring &str)
{
	m_sContent += str;
	return *this;
}
MD5Stream &MD5Stream::operator <<(int i)
{
	char s[32];
	sprintf(s,"%d",i);
	m_sContent += s;
	return *this;
}

bool ComputeMD5(const char *pinput,int len,char *poutput)
{
	char m_Buf[32];
	MD5_CTX	m_Context;
	MD5_Init(&m_Context);
	MD5_Update(&m_Context,(UInt8*)pinput,len);
	MD5_Final((UInt8*)m_Buf, &m_Context);
	char* p = m_Buf;	
	for (int i=0; i < 16; i++)
	{
		unsigned char temp = *p++;
	   
		int hi = (temp & 0xf0)>>4;
		int low = (temp & 0xf);      
		poutput[i*2] = hexmap[hi];
		poutput[i*2+1] = hexmap[low];
	}	
	return true;
}

